package org.mybatis.generator.codegen.mybatis3.javamapper.elements.suffix;

import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.codegen.mybatis3.SuffixUtil;
import org.mybatis.generator.codegen.mybatis3.javamapper.elements.AbstractJavaMapperMethodGenerator;

import java.util.Set;
import java.util.TreeSet;

/*********************************************************
 * 文件名称：SuffixInsertSelectiveMethodGenerator.java
 * 系统名称：cxycloud
 * 模块名称：org.mybatis.generator.codegen.mybatis3.javamapper.elements.suffix
 * 功能说明：带后缀得条件插入生成器
 * 开发人员 @author：caoxy
 * 开发时间 @date：2023/8/29 9:47
 * 修改记录：程序版本  修改日期  修改人员  修改单号  修改说明
 *********************************************************/
public class SuffixInsertSelectiveMethodGenerator extends AbstractJavaMapperMethodGenerator {


    @Override
    public void addInterfaceElements(Interface interfaze) {
        Method method = new Method(introspectedTable.getInsertSelectiveStatementId());
        method.setName(SuffixUtil.getSuffixMethodName(method.getName(),introspectedTable));
        method.setReturnType(FullyQualifiedJavaType.getIntInstance());
        method.setVisibility(JavaVisibility.PUBLIC);
        method.setAbstract(true);

        FullyQualifiedJavaType parameterType = introspectedTable.getRules().calculateAllFieldsClass();

        Set<FullyQualifiedJavaType> importedTypes = new TreeSet<>();
        importedTypes.add(parameterType);
        importedTypes.add(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Param"));
        method.addParameter(new Parameter(parameterType, "row","@Param(\"row\")")); //$NON-NLS-1$
        SuffixUtil.addSuffixParam(importedTypes,method);
        context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);

        addMapperAnnotations(method);

        if (context.getPlugins().clientInsertSelectiveMethodGenerated(method, interfaze, introspectedTable)
                && SuffixUtil.isEnableSuffix(introspectedTable)) {
            addExtraImports(interfaze);
            interfaze.addImportedTypes(importedTypes);
            // 删除原方法
            SuffixUtil.deleteOldMethod(interfaze, method);
            interfaze.addMethod(method);
        }
    }

    public void addMapperAnnotations(Method method) {
        // extension point for subclasses
    }

    public void addExtraImports(Interface interfaze) {
        // extension point for subclasses
    }
}
